<template>
  <!-- 门资料管理 -->
  <div class="door">
    <!-- 门资料管理显示 -->
    <div class="my_table">
      <el-table
        v-loading="loading"
        :data="detailslist"
        ref="table"
        border
        highlight-current-row
        style="width: 100%"
        max-height="450"
      >
        <el-table-column prop="doorCode" label="门编号" align="center"></el-table-column>
        <el-table-column prop="doorName" label="门名称" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增门资料 -->
    <el-dialog
      :before-close="closeDialog"
      width="500px"
      center
      title="修改门名称"
      :close-on-click-modal="false"
      :visible.sync="addDialogFormVisible"
      append-to-body
    >
      <el-form :model="adddataForm" :rules="adddataRule" ref="adddataForm" label-width="80px">
        <el-form-item label="门编号" prop="doorCode">
          <el-col :span="15">
            <el-input v-model="adddataForm.doorCode" size="mini" :disabled="true"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="门名称" prop="doorName">
          <el-col :span="15">
            <el-input v-model="adddataForm.doorName" placeholder="门名称" size="mini"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addvisible()">取消</el-button>
        <el-button type="primary" @click="addFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import bus from "@/bus.js";
export default {
  data() {
    return {
      loading: false,
      detailslist: [],
      // 添加弹窗
      addDialogFormVisible: false,
      // 模板名称
      adddataForm: {
        doorName: "",
        doorCode: ""
      },
      // 模板规则
      adddataRule: {
        doorName: [
          { required: true, message: "门名称不能为空", trigger: "blur" },
           { max: 20, message: '长度在20个字符以下', trigger: 'blur' }
        ]
      },
    };
  },
  created() {
    // 门信息获取
    this.getDoorlist();
  },
  mounted() {
    bus.$on("listenToA", this.getHeaderDate);
  },
  props: ["unitGuid", "terminalGuid"],
  methods: {
    // 门信息获取
    getDoorlist() {
      this.loading = true;
      this.$http({
        url: this.$http.adornUrl("/terminal/entrance/entrance-door-search"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            terminalGuid: this.terminalGuid,
            unitGuid: this.unitGuid,
            terminalType: 21
          },
          messageType: 0
        })
      }).then(({ data }) => {
        this.loading = false;
        console.log(data.messageContent);
        this.$emit('refreshDataList')
        // 门资料信息
        this.detailslist = data.messageContent;
      });
    },
    // 编辑
    handleClick(row) {
      this.addDialogFormVisible = true;
      this.adddataForm = { ...row };
    },
    // 添加门资料取消按钮
    addvisible() {
      this.addDialogFormVisible = false;
      this.$refs["adddataForm"].resetFields();
    },
    closeDialog(done) {
      this.$refs["adddataForm"].resetFields();
      done();
    },
    // 新增模板
    addFormSubmit() {
      this.$refs["adddataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl("/terminal/entrance/entrance-door-update"),
            method: "post",
            data: this.$http.adornData({
              messageContent: {
                    doorGuid: this.adddataForm.doorGuid,
                    doorCode: this.adddataForm.doorCode,
                    doorName: this.adddataForm.doorName,
                    openType: 0
                // terminalGuid: this.terminalGuid,
                // unitGuid: this.unitGuid
              },
              messageType: this.messageType
            })
          }).then(({ data }) => {
            if (data.messageCode == 200) {
              this.$message({
                showClose: true,
                message: "修改成功",
                type: "success"
              });
              // 更新门禁下的门资料
              this.getDoorlist();
              this.addDialogFormVisible = false;
            } else {
              this.$message({
                showClose: true,
                message: data.errorMessage,
                type: "error"
              });
            }
          });
        }
      });
    },  
  }
};
</script>

<style lang="scss" scoped>
.page {
  display: flex;
  justify-content: center;
  align-items: center;
}
.door {
  margin-left: -50px;
  padding:20px;
  padding-bottom:30px;
}
.TimeWeek {
  padding: 20px 0 0;
}
.el-dialog--center .el-dialog__body {
  padding: 0 50px;
}
// 隐藏纵向滚动条
</style>